package com.jk.youxuanmall.coupon.controller;

import java.util.Arrays;

import com.jk.youxuanmall.common.pojo.PagedResponse;
import com.jk.youxuanmall.common.pojo.ResponseResult;
import com.jk.youxuanmall.common.pojo.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.jk.youxuanmall.coupon.entity.CouponEntity;
import com.jk.youxuanmall.coupon.service.CouponService;



/**
 * 优惠券主表
 *
 * @author yangzhichao
 * @email 780979437@qq.com
 * @date 2025-07-30 10:35:54
 */
@RestController
@RequestMapping("coupon/coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;

    /**
     * 测试接口调用
     */
    @GetMapping(value = "/findCouponByUsername")
    public ResponseResult findCouponByUsername(@RequestParam(value = "username") String username) {
        CouponEntity couponEntity = new CouponEntity();
        couponEntity.setCouponName("满100减50");

        return new ResponseResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), Arrays.asList(couponEntity));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ResponseResult<PagedResponse<CouponEntity>> list(CouponEntity couponEntity, Integer pageNum, Integer pageSize){
        return new ResponseResult<>(200, "查询成功！", couponService.queryPage(couponEntity, pageNum, pageSize));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public ResponseResult<CouponEntity> info(@PathVariable("id") Long id){
		CouponEntity coupon = couponService.getById(id);

        return new ResponseResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), coupon);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResponseResult save(@RequestBody CouponEntity coupon){
        try {
            couponService.save(coupon);
            return new ResponseResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg());
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResponseResult update(@RequestBody CouponEntity coupon){
        try {
            couponService.updateById(coupon);
            return new ResponseResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg());
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ResponseResult delete(@RequestBody Long[] ids){
        try {
            couponService.removeByIds(Arrays.asList(ids));
            return new ResponseResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg());
        }
    }

}
